package com.netbetter.langzheng.commons.gears.innerbo.impl;

import java.util.List;
import java.util.Map;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.gears.client.database.Database;
import com.netbetter.langzheng.commons.gears.dto.ParametersDTO;
import com.netbetter.langzheng.commons.gears.dto.SqlStateDTO;
import com.netbetter.langzheng.commons.gears.innerbo.Gears2DataObjectBO;
import com.netbetter.langzheng.commons.gears.utils.GearsUtils;

/**
 * 将gears表中数据读取出来
 * 
 * @author jim
 */
public class Gears2DataObjectBOImpl implements Gears2DataObjectBO {

  private ParametersDTO parameter;

  public void setParameter(ParametersDTO parameter) {
    this.parameter = parameter;
  }

  public List<Map<String, String>> getLocalData() {
    Database localDB = parameter.getLocalDB();
    try {
      SqlStateDTO sqlDto = GearsUtils.getSelectByParameterDTO(parameter);
      if (Log.isDebugEnabled()) {
        StringBuffer buffer = new StringBuffer(" excute sql:" + sqlDto.getSql());
        for (int i = 0; i < sqlDto.getValues().length; i++) {
          buffer.append(sqlDto.getValues()[i]).append(",");
        }
        Log.debug("sql" + buffer.toString());
      }
      return GearsUtils.getListForSql(localDB, sqlDto);
    } catch (Exception error) {
      Log.error("excute  sql:", error);
    }
    return null;
  }

  public boolean isExistData() {
    Database localDB = parameter.getLocalDB();
    try {
      SqlStateDTO sqlDto = GearsUtils.getSelectCountByParameterDTO(parameter);
      if (Log.isDebugEnabled()) {
        StringBuffer buffer = new StringBuffer(" excute sql:" + sqlDto.getSql());
        for (int i = 0; i < sqlDto.getValues().length; i++) {
          buffer.append(sqlDto.getValues()[i]).append(",");
        }
        Log.debug("sql" + buffer.toString());
      }
      int count = GearsUtils.getCountForSql(localDB, sqlDto);
      return count > 0;
    } catch (Exception error) {
      Log.error("excute  sql:", error);
    }
    return false;
  }
}
